﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class ListShowProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Them du lieu vao cat 1
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", "0");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate1.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate1.Items.Add(lt);
                }
                Cate1.SelectedIndex = 0;

            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            Cate2.Items.Clear();
            Cate3.Items.Clear();

            int index = Cate1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate2.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate2.Items.Add(lt);
                }
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_ThietKeWebsite","Category_ID3", id.ToString());
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();

            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
              // Chon danh muc cap 1 load danh muc cap 2
            Cate3.Items.Clear();
            int index = Cate2.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate2.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate3.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate3.Items.Add(lt);
                }
                 dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_ThietKeWebsite","Category_ID3", id.ToString());
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();

            }
        }

        protected void Cate3_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Load san pham
            int id = Convert.ToInt32(Cate3.SelectedValue);
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_ThietKeWebsite","Category_ID3", id.ToString());
            rpt_Sanpham.DataSource = dt;
            rpt_Sanpham.DataBind();


        }

        protected void rpt_Sanpham_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());

                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_ThietKeWebsite", "ThietKeWebsite_ID", id.ToString());
                string bigimage = dt.Rows[0]["ThietKeWebsite_Image"].ToString();

                

                File.Delete(MapPath(bigimage));
                // Xoa
            
                DAL.DCollector.DBCollector.Instance.StoredDelete("tbl_ThietKeWebsite","ThietKeWebsite_ID" ,id.ToString());

                // Xoa ảnh sp           

                Response.Redirect("/Admin/Index.aspx?panel=1&sub=1");
            }
        }

        protected void Cate1_TextChanged(object sender, EventArgs e)
        {
             // Chon danh muc cap 1 load danh muc cap 2
            Cate2.Items.Clear();
            Cate3.Items.Clear();

            int index = Cate1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate2.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate2.Items.Add(lt);
                }
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_ThietKeWebsite","Category_ID3", id.ToString());
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
            }
        }
    }
}